﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormChiTietKhachHang : Form
    {
        private NIEN_LUAN.Db.DataContentDataContext db = new NIEN_LUAN.Db.DataContentDataContext();

        private FormQuanLyKhachHang frmQuanlykhachhang;
        private FormTimChiTietKhachHang frmTimKiemChiTietKhachHang;
        private FormLapChungTu frmLapChungTu;
        private FormTimKiemKhachHang frmTimKhachHang;
        public Db.KHACHHANG KhachHangMuonSua;
   
        public FormChiTietKhachHang(FormQuanLyKhachHang frm)
        {
            frmQuanlykhachhang = frm;
            InitializeComponent();
        }

        public FormChiTietKhachHang(Db.KHACHHANG model,FormQuanLyKhachHang frm)
        {

            InitializeComponent();
            this.txtMaKH.Text = model.MaKhachHang;
            this.txtMaKH.Enabled = false;
            this.txtDChiKH.Text = model.DiaChi;
            this.txtSDTKH.Text = model.SoDienThoai;
            this.txtTenKH.Text = model.TenKhachHang;
            KhachHangMuonSua = model;
            frmQuanlykhachhang = frm;        
        }

        public FormChiTietKhachHang(Db.KHACHHANG model, FormTimKiemKhachHang frm)
        {

            InitializeComponent();
            this.txtMaKH.Text = model.MaKhachHang;
            this.txtMaKH.Enabled = false;
            this.txtDChiKH.Text = model.DiaChi;
            this.txtSDTKH.Text = model.SoDienThoai;
            this.txtTenKH.Text = model.TenKhachHang;
            KhachHangMuonSua = model;
            frmTimKhachHang = frm;
        }

        public FormChiTietKhachHang(FormLapChungTu frm)
        {
            this.frmLapChungTu = frm;
            InitializeComponent();
        }

        public FormChiTietKhachHang(FormTimChiTietKhachHang frm)
        {
            this.frmTimKiemChiTietKhachHang = frm;
            InitializeComponent();        
        }
       
        private void btnThoatCTKH_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;//khi click vao day thi tra ve dialog result la cancel
            this.Close();
        }

        private void btnLuuCTKH_Click(object sender, EventArgs e)
        {
            //kiem tra tinh hop le cua du lieu nhap vao
            if (string.IsNullOrEmpty(this.txtMaKH.Text.Trim()))
            {
                MessageBox.Show("Mã khách hàng không được rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtTenKH.Text.Trim()))
            {
                MessageBox.Show("Tên khách hàng rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 this.txtTenKH.Focus();
                 return;
            }

            if (string.IsNullOrEmpty(this.txtDChiKH.Text.Trim()))
            {
                MessageBox.Show("Địa chỉ khách hàng rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDChiKH.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtSDTKH.Text.Trim()))
            {
                MessageBox.Show("Số điện thoại khách hàng rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtSDTKH.Focus();
                return;
            }

            var khachhang = new NIEN_LUAN.Db.KHACHHANG
            {
                MaKhachHang = this.txtMaKH.Text,
                TenKhachHang = this.txtTenKH.Text,               
                DiaChi = this.txtDChiKH.Text,               
                SoDienThoai = this.txtSDTKH.Text,              
            };

            if (frmQuanlykhachhang != null && KhachHangMuonSua == null)//chi them o bang quan ly kh
            {
                var kh = (from m in db.KHACHHANGs
                          where m.MaKhachHang == this.txtMaKH.Text.Trim()
                          select m).ToList();
                if (kh.Count > 0)
                {
                    MessageBox.Show("Ma trung!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                db.KHACHHANGs.InsertOnSubmit(khachhang);
                db.SubmitChanges();
                this.frmQuanlykhachhang.KhachHangAdd = khachhang;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            
            if (frmTimKiemChiTietKhachHang != null)//them khach hang moi tu bang lap chung tu
            {
                var kh = (from m in db.KHACHHANGs
                          where m.MaKhachHang == this.txtMaKH.Text.Trim()
                          select m).ToList();
                if (kh.Count > 0)
                {
                    MessageBox.Show("Ma trung!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                db.KHACHHANGs.InsertOnSubmit(khachhang);
                db.SubmitChanges();
                this.frmTimKiemChiTietKhachHang.kHACHHANGBindingSource.Add(khachhang);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }


            if (frmLapChungTu != null && frmQuanlykhachhang ==null && KhachHangMuonSua==null )//them khach hang moi tu bang lap chung tu
            {
                var kh = (from m in db.KHACHHANGs
                          where m.MaKhachHang == this.txtMaKH.Text.Trim()
                          select m).ToList();
                if (kh.Count > 0)
                {
                    MessageBox.Show("Mã trùng!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                db.KHACHHANGs.InsertOnSubmit(khachhang);
                db.SubmitChanges();
                this.frmLapChungTu.khachangAdd = khachhang;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }

            if (KhachHangMuonSua != null&&frmQuanlykhachhang!=null)//sua chi tiet khach hang 
            {
                ///
                var _khachhang = db.KHACHHANGs.Single(m => m.MaKhachHang == khachhang.MaKhachHang);
                _khachhang.SoDienThoai = khachhang.SoDienThoai;
                _khachhang.TenKhachHang = khachhang.TenKhachHang;
                _khachhang.DiaChi = khachhang.DiaChi;
                db.SubmitChanges();
                this.frmQuanlykhachhang.KhachHangSua = _khachhang;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }

            if (KhachHangMuonSua != null && frmTimKhachHang  != null)//sua chi tiet khach hang 
            {
                ///
                var _khachhang = db.KHACHHANGs.Single(m => m.MaKhachHang == khachhang.MaKhachHang);
                _khachhang.SoDienThoai = khachhang.SoDienThoai;
                _khachhang.TenKhachHang = khachhang.TenKhachHang;
                _khachhang.DiaChi = khachhang.DiaChi;
                db.SubmitChanges();
                this.frmTimKhachHang.KhachHangSua = _khachhang;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
